package com.info.zhiduoduo.common.baidusdk;

import com.alibaba.fastjson.JSON;
import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import com.info.zhiduoduo.common.baidusdk.configs.BaiduSdkConfig;
import com.info.zhiduoduo.common.baidusdk.domain.BankcardResponse;
import com.info.zhiduoduo.common.utils.NumberUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class BankcardService {

	@Autowired
	BaiduSdkConfig baiduSdkConfig;

	public BankcardResponse.DataDTO getBankcardInfo(String bankcard) {
		try {
			String path = "http://gwgp-fn4psdsjo56.n.bdcloudapi.com/bankcard/area";
			ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
			request.setCredentials(baiduSdkConfig.getBceAccessKey(), baiduSdkConfig.getBceAppSecret());
			request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
			request.addQueryParameter("bankcard", bankcard);

			ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

			ApiExplorerResponse response = client.sendRequest(request);
			// 返回结果格式为Json字符串
			System.out.println(response.getResult());
			BankcardResponse bankcardResponse = JSON.parseObject(response.getResult(), BankcardResponse.class);
			if (bankcardResponse != null && NumberUtil.equals(bankcardResponse.getCode(), 200)) {
				return bankcardResponse.getData();
			}
		} catch (Exception e) {
			log.error("获取银行卡开户地信息报错", e);
		}
		return null;
	}

	public static void main(String[] args) {
		String path = "http://gwgp-fn4psdsjo56.n.bdcloudapi.com/bankcard/area";
		ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
		request.setCredentials("16245d8930504465b7d09b844e9687db", "a33ffc1fed19413396896379fbc4b751");

		request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");

		request.addQueryParameter("bankcard", "6226630602760232");

		ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

		try {
			ApiExplorerResponse response = client.sendRequest(request);
			// 返回结果格式为Json字符串
			System.out.println(response.getResult());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
